package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysLog;
import com.itheima.service.RoleService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.Result;
import com.itheima.utils.ResultUtil;
import com.itheima.utils.ResultUtils;
import com.itheima.vo.RoleVO;
import com.itheima.vo.SysLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api(tags = "角色相关接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /*
    * 新增角色信息
    * */
    @PostMapping("/add")
    @ApiOperation("新增角色信息")
    public String add(@RequestBody RoleVO roleVO){
        return roleService.addrole(roleVO);
    }

    /*
    * 删除角色信息
    * */
    @PostMapping("/delete")
    @ApiOperation("删除角色信息")
    public String delete(@RequestBody RoleVO roleVO){
        if (roleVO.getUid() == null || "".equals(roleVO.getUid())) {
            return ResultUtils.result("error", MessageConstant.ADD_UID);
        }
        roleService.delete(roleVO);
        return ResultUtils.result("success", MessageConstant.ROLE_DELETE_SUCCESS);
    }

    /*
    * 更新角色信息
    * */
    @PostMapping("/edit")
    @ApiOperation("更新角色信息")
    public Result edit(@RequestBody Role role){
        return roleService.edit(role);
    }

    /*
    * 获取角色信息列表
    * */
    @PostMapping("/getList")
    public String getList(@RequestBody(required = false) RoleVO roleVO) {
        Page<Role> logList = roleService.findList(roleVO);
        Map<String, Object> map = new HashMap();
        map.put("records",logList.getResult());
        map.put("total",logList.getTotal());
        map.put("size",logList.getPageSize());
        map.put("current",logList.getPageNum());
        map.put("orders","");
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return ResultUtil.result("success", map);
    }
}
